﻿using AccessibilityTools.DB;
using AnDaPPE.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnDaPPE.MySqlDal
{
   public class ErrorProtectionInfoService: BaseRepository<ErrorProtectionInfo>
    {
        #region Method [8]

        /// <summary>
        /// 查询表格中最大的ID
        /// </summary>
        /// <returns></returns>
        public int GetMaxId()
        {
            int maxId = Queryable().Max(it => it.Id);
            return maxId;
        }

        /// <summary>
        /// 数据记录数量
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            int total = GetCountAsync(it => it.IsDeleted == 0).Result;
            return total;
        }

        /// <summary>
        /// 数据记录数量
        /// </summary>
        /// <returns></returns>
        public int GetCount(string sqlWhere)
        {
            int total = QueryListByClauseAsync(sqlWhere).Result.Count;
            return total;
        }

        /// <summary>
        /// 根据ID 判断数据是否存在
        /// </summary>
        /// <returns></returns>
        public bool Exist(int ID)
        {
            bool result = ExistsAsync(it => it.Id == ID).Result;
            return result;
        }

        /// <summary>
        /// 增加对象信息
        /// </summary>
        /// <param name="boxsettinginfo"Instance>对象实例</param>
        /// <returns></returns>
        public int Add(ErrorProtectionInfo Instance)
        {
            Instance.IsDeleted = 0;
            int count = InsertAsync(Instance).Result;
            return count;
        }

        /// <summary>
        /// 增加对象列表信息
        /// </summary>
        /// <param name="boxsettinginfo"Instance>对象实例列表</param>
        /// <returns>返回插入成功的个数，如果遇到不能成功插入的中断插入</returns>
        public int Add(List<ErrorProtectionInfo> Instances)
        {
            int successCount = 0;
            successCount = InsertAsync(Instances).Result;
            return successCount;
        }

        /// <summary>
        /// 删除数据 不是软删除慎用！
        /// </summary>
        /// <param name="id">对象Id</param>
        /// <returns></returns>
        public bool Delete(int ID)
        {
            bool result = FakeDeleteByIdAsync(ID).Result;
            return result;
        }

        /// <summary>
        /// 更新对象信息
        /// </summary>
        /// <param name="boxsettinginfoInstances">对象实例</param>
        /// <returns></returns>
        public int Update(ErrorProtectionInfo Instance)
        {
            bool result = UpdateAsync(Instance).Result;
            if (result)
            {
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 查询有关二维码的信息
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public ErrorProtectionInfo QuerySingle(string sn)
        {
            List<ErrorProtectionInfo> instance = QueryListByClauseAsync(p => p.ProtectionSn == sn && p.IsDeleted == 0).Result;
            if (instance.Any())
            {
                return instance[0];
            }
            return null;
        }
        /// <summary>
        /// 更新对象信息
        /// </summary>
        /// <param name="boxsettinginfoInstances">对象实例</param>
        /// <returns></returns>
        public int Update(List<ErrorProtectionInfo> Instances)
        {

            bool result = UpdateAsync(Instances).Result;
            if (result)
            {
                return Instances.Count;
            }
            return 0;
        }


        /// <summary>
        /// 根据sql 查询对象集合
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="ps">参数</param>
        /// <returns></returns>
        public List<ErrorProtectionInfo> GetList(string sql)
        {
            List<ErrorProtectionInfo> boxsettinginfoInstance = new List<ErrorProtectionInfo>();
            boxsettinginfoInstance = SqlQueryable(sql).Result;
            if (boxsettinginfoInstance.Count > 0 && boxsettinginfoInstance != null)
            {
                return boxsettinginfoInstance;
            }
            return null;
        }

        /// <summary>
        /// 查询对象集合
        /// </summary>
        /// <returns></returns>
        public List<ErrorProtectionInfo> GetList()
        {
            List<ErrorProtectionInfo> boxsettinginfoInstance = QueryAsync().Result;
            if (boxsettinginfoInstance.Count > 0 && boxsettinginfoInstance != null)
            {
                return boxsettinginfoInstance;
            }
            return null;
        }

        /// <summary>
        /// 根据IsEnabled查询对象集合
        /// </summary>
        /// <param name="IsDeleted">软删除标志位</param>
        /// <returns></returns>
        public List<ErrorProtectionInfo> GetList(int IsEnabled)
        {
            List<ErrorProtectionInfo> Instance = QueryListByClauseAsync(it => it.IsDeleted == IsEnabled).Result;
            if (Instance.Count > 0 && Instance != null)
            {
                return Instance;
            }
            return null;
        }
        /// <summary>
        /// 更新对象按照时间查询
        /// </summary>
        /// <param name="productiondetailsinfoInstances">对象实例</param>
        /// <returns></returns>
        public List<ErrorProtectionInfo> QueryYieldInfoByDate(DateTime dtStart, DateTime dtEnd)
        {
            List<ErrorProtectionInfo> ins = QueryListByClause(p => p.CreationTime >= dtStart && p.CreationTime <= dtEnd && p.IsDeleted == 0);
            if (ins.Any())//判断对象ins是否为空
            {
                return ins;
            }
            return null;
        }

        #endregion

        /// <summary>
        /// 删除数据 不是软删除慎用！
        /// </summary>
        /// <param name="where">表达式</param>
        /// <returns></returns>
        public bool Delete(string sn)
        {
            //public async Task<bool> FakeDeleteAsync(Expression<Func<T, bool>> where)
            //异步方法需要用到.Result
            bool b = FakeDeleteAsync(p => p.ProtectionSn == sn && p.IsDeleted == 0).Result;
            if (b)
            {
                return b;
            }
            return false;

        }

    }
}
